<template>

    <body onselectstart="return false">
        <div v-transfer-dom>
            <x-dialog v-model="show" class="dialog-demo">
                <div class="img-box">
                    <img src="~assets/x3//ico/shop-header.png" style="max-width:100%">
                </div>
                <div @click="show=false">
                    <span class="vux-close"></span>
                </div>
            </x-dialog>
        </div>
        <div id="signature-pad" class="m-signature-pad">
            <div class="m-signature-pad--body">
                <canvas></canvas>
            </div>
            <div class="m-signature-pad--footer">
                <div class="description">请在空白处签名</div>
                <div class="left">
                    <x-button @click.native="clear" mini class="button clear" type="warn" data-action="clear">清除</x-button>
                </div>
                <div class="right">
                    <x-button mini class="button save" data-action="save-png" type="primary">保存PNG</x-button>
                    <x-button mini class="button save" data-action="save-svg" type="primary">保存SVG</x-button>
                </div>
            </div>
        </div>

    </body>
</template>

<script>
import SignaturePad from "signature_pad";     // "signature_pad": "^2.3.0",
import { XButton, TransferDomDirective as TransferDom, XDialog } from 'vux'
export default {
    directives: {
        TransferDom
    },
    components: {
        XButton,
        XDialog
    },
    data() {
        return {
            show: false
        }
    },
    methods: {
        clear(signaturePad) {

        },
        resizeCanvas(canvas, signaturePad) {
            var ratio = Math.max(window.devicePixelRatio || 1, 1);
            canvas.width = canvas.offsetWidth * ratio;
            canvas.height = canvas.offsetHeight * ratio;
            canvas.getContext("2d").scale(ratio, ratio);
            signaturePad.clear();
        }
    },
    mounted() {
        var wrapper = document.getElementById("signature-pad");
        var clearButton = wrapper.querySelector("[data-action=clear]");
        var savePNGButton = wrapper.querySelector("[data-action=save-png]");
        var saveSVGButton = wrapper.querySelector("[data-action=save-svg]");
        var canvas = wrapper.querySelector("canvas");
        var signaturePad = new SignaturePad(canvas);
        window.onresize = SignaturePad.resizeCanvas;
        this.resizeCanvas(canvas, signaturePad);

        // clearButton.addEventListener("click", function(event) {
        //     signaturePad.clear();
        // });

        // savePNGButton.addEventListener("click", function(event) {
        //     if (signaturePad.isEmpty()) {
        //         console.log("Please provide signature first.");
        //     } else {
        //         console.log(signaturePad.toDataURL());
        //         // window.open(signaturePad.toDataURL());
        //     }
        // });

        saveSVGButton.addEventListener("click", function (event) {
            if (signaturePad.isEmpty()) {
                console.log("Please provide signature first.");
                // alert("Please provide signature first.");
            } else {
                console.log(signaturePad.toDataURL('image/svg+xml'));
                // window.open(signaturePad.toDataURL('image/svg+xml'));
            }
        });
    }
}
</script>

<style lang="less">
// .vux-close {
//     position: relative;
//     display: inline-block;
//     vertical-align: middle;
//     color: #999;
//     width: 24px;
//     height: 24px;
//     &:before,
//     &:after {
//         content: '';
//         position: absolute;
//         left: 0;
//         top: 11px;
//         width: 24px;
//         height: 1px;
//         background-color: currentColor;
//         transform: rotate(-45deg);
//     }
//     &:after {
//         transform: rotate(45deg);
//     }
// }
// .dialog-demo {
//     .weui-dialog {
//         border-radius: 8px;
//         padding-bottom: 8px;
//     }
//     .dialog-title {
//         line-height: 30px;
//         color: #666;
//     }
//     .img-box {
//         height: 350px;
//         overflow: hidden;
//     }
//     .vux-close {
//         margin-top: 8px;
//         margin-bottom: 8px;
//     }
// }
.m-signature-pad {
  position: absolute;
  font-size: 10px;
  width: 700px;
  height: 400px;
  top: 50%;
  left: 50%;
  margin-left: -350px;
  margin-top: -200px;
  border: 1px solid #e8e8e8;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.08) inset;
  border-radius: 4px;
}

.m-signature-pad:before,
.m-signature-pad:after {
  position: absolute;
  z-index: -1;
  content: "";
  width: 40%;
  height: 10px;
  left: 20px;
  bottom: 10px;
  background: transparent;
  -webkit-transform: skew(-3deg) rotate(-3deg);
  -moz-transform: skew(-3deg) rotate(-3deg);
  -ms-transform: skew(-3deg) rotate(-3deg);
  -o-transform: skew(-3deg) rotate(-3deg);
  transform: skew(-3deg) rotate(-3deg);
  box-shadow: 0 8px 12px rgba(0, 0, 0, 0.4);
}

.m-signature-pad:after {
  left: auto;
  right: 20px;
  -webkit-transform: skew(3deg) rotate(3deg);
  -moz-transform: skew(3deg) rotate(3deg);
  -ms-transform: skew(3deg) rotate(3deg);
  -o-transform: skew(3deg) rotate(3deg);
  transform: skew(3deg) rotate(3deg);
}

.m-signature-pad--body {
  position: absolute;
  left: 20px;
  right: 20px;
  top: 20px;
  bottom: 80px;
  border: 1px solid #f4f4f4;
}

.m-signature-pad--body canvas {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-radius: 4px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.02) inset;
}

.m-signature-pad--footer {
  position: absolute;
  left: 20px;
  right: 20px;
  bottom: 20px;
  height: 60px;
}

.m-signature-pad--footer .description {
  color: #c3c3c3;
  text-align: center;
  font-size: 1.2em;
  margin-top: 1em;
}

.m-signature-pad--footer .left,
.right {
  position: absolute;
  bottom: 0;
}

.m-signature-pad--footer .left {
  left: 0;
}

.m-signature-pad--footer .right {
  right: 0;
}

@media screen and (max-width: 1024px) {
  .m-signature-pad {
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    height: auto;
    min-width: 250px;
    min-height: 140px;
    margin: 5%;
  }
  #github {
    display: none;
  }
}

@media screen and (min-device-width: 768px) and (max-device-width: 1024px) {
  .m-signature-pad {
    margin: 10%;
  }
}

@media screen and (max-height: 320px) {
  .m-signature-pad--body {
    left: 0;
    right: 0;
    top: 0;
    bottom: 32px;
  }
  .m-signature-pad--footer {
    left: 20px;
    right: 20px;
    bottom: 4px;
    height: 28px;
  }
  .m-signature-pad--footer .description {
    font-size: 1em;
    margin-top: 1em;
  }
}
</style>










